var permissionSetting = {
	view : {
		selectedMulti : false
	},
	check: {
		enable: true,
		nocheckInherit: false
	},
	data: {
		simpleData: {
			enable: false
		}
	},
	callback : {
		onCheck : onCheck
	}
}, roleSetting = {
		view : {
			selectedMulti : false
		},
		data: {
			key : {
				name : 'roleName'
			},
			simpleData: {
				enable: true
			}
		},
		callback : {
			onClick : roleNodeSelect
		}
}, zNodes, zNodes2, roles;
//获取菜单树
function getNodes() {
	$.ajax({
		url : "system/getMenuModule",
		async : false,
		timeout : 3000,
		dataType : "json",
		success : function(data) {
			if (data) {
				if (data.code == 100) {
					zNodes = data.message;
				} else {
					showAlert(data.message);
				}
			} else {
				showAlert("获取列表失败!");
			}
		},
		error : function() {
			showAlert("获取菜单失败!");
		}
	});
}
function getRoles(){
	$.ajax({
		url : "system/getRoles",
		async : false,
		timeout : 3000,
		dataType : "json",
		success : function(data){
			if(data&&data.code == 100){
				zNodes2 = data.message;
			}else{
				showAlert("获取角色信息失败!");
			}
		},
		error : function(){
			showAlert("获取角色信息失败!");
		}
	});
}
function onCheck(treeId, treeNode){
	var treeObj = $.fn.zTree.getZTreeObj("tree2"), node = treeObj.getSelectedNodes()[0];
	if(node){
		var cn = $.fn.zTree.getZTreeObj("permissionTree").getCheckedNodes(true), arr = [];
		$.each(cn, function(i,n){
			if(n.id){
				arr.push(n.id);
			}
		});
		node.permissions = arr;
	}
}
function roleNodeSelect(e, treeId, treeNode){
	var nodes = $.fn.zTree.getZTreeObj("permissionTree").getNodesByFilter(function(node){
		return node.level==2;
	});
	permissionCheck(treeNode, nodes);
}
function permissionCheck(treeNode, nodes){
	if(nodes.length>0){
		var permission = treeNode.permissions, pTree = $.fn.zTree.getZTreeObj("permissionTree");
		pTree.checkAllNodes(false);
		if(permission.length>0){
			permission = ","+permission.toString()+",";
			var t;
			for(var i=0;i<nodes.length;i++){
				t = nodes[i];
				if(permission.indexOf(","+t.id+",")>=0){
					pTree.checkNode(t, true);
				}
			}
		}
	}
	return false;
}
function saveChange(){
	var pTree = $.fn.zTree.getZTreeObj("tree2"), nodes = pTree.getSelectedNodes()[0], permissionArr = [];
	if(nodes){
		$.ajax({
			url : "system/changePermission",
			async : false,
			data : {"role":nodes.id,"permission":nodes.permissions.toString()},
			timeout : 3000,
			dataType : "json",
			success : function(data){
				if(data&&data.code == 100){
					showAlert("修改完成!");
				}else{
					showAlert("修改失败!");
				}
			},
			error : function(){
				showAlert("修改失败!");
			}
		});
	}else{
		alert("0");
	}
}
$(document).ready(function() {
	getNodes();
	getRoles();
	$.fn.zTree.init($("#tree2"), roleSetting, zNodes2);
	$.fn.zTree.init($("#permissionTree"), permissionSetting, zNodes);//
	$.fn.zTree.getZTreeObj("permissionTree").expandAll(true);
	var treeObj = $.fn.zTree.getZTreeObj("tree2");
	var nodes = treeObj.getNodes();
	if (nodes.length>0) {
		var temp = nodes[0];
		treeObj.selectNode(temp);
		roleNodeSelect(null, "tree2", temp);
	}
	$("#saveChange").bind("click", saveChange);
});